
module Distribution.FractalDist (calculateK) where

-- imports
import Numeric.GSL.Fitting.Linear
import Data.Packed.Vector
import Numeric.Container

logscaleDist :: [(Double, Double)] -> [(Double, Double)]
logscaleDist xs = map makeLog xs
        where makeLog (chng, cnt) = (chng, log cnt)

vectorize :: [(Double, Double)] -> (Vector Double, Vector Double)
vectorize = buildVectors . unzip
        where  buildVectors (x, y)     = (fromList x, fromList y)

makeK :: (Vector Double, Vector Double) -> (Double, Double, Double, Double, Double, Double)
makeK (x, y) = linear x y

expK :: (Vector Double, Vector Double) -> (Double, Double, Double, Double, Double, Double)
expK (x, y) = (0.0, 1.0/k, 0.0, 0.0, 0.0, 0.0)
        where k = (sumElements (mul x y))/(sumElements y)

cleanUp = id

calculateK :: [(Double, Double)] -> (Double, Double, Double, Double, Double, Double)
calculateK x
        | null x        = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
        | otherwise     = (expK . vectorize . logscaleDist . cleanUp) x

delog (c, k, cov00, cov01, cov11, chiSq) = (exp c, k, cov00, cov01, cov11, chiSq)
